package com.hujiang.basic.search.search.controller;

import com.hujiang.basic.search.api.SearchService;
import com.hujiang.basic.search.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api("查询与搜索 API")
@RequestMapping(value = "/v1/doc")
@Slf4j
public class SearchController {

    @Resource
    private SearchService searchService;

    @ApiOperation(value = "查询文档", httpMethod = "POST", response = HJResponse.class, notes = "get doc")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public HJResponse get(@RequestBody HJGetRequest getReqeust) {
        log.info(getReqeust.toString());
        return HJResponse.ok(searchService.get(getReqeust));
    }

    @ApiOperation(value = "搜索文档", httpMethod = "POST", response = HJResponse.class, notes = "search doc")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public HJResponse search(@RequestBody HJSearchRequest request) {
        log.info(request.toString());
        HJSearchResponse response =  searchService.search(request);
        if(response.getStatus()==-1){
            return HJResponse.fail(response.getMessage());
        }
        return HJResponse.ok(searchService.search(request));
    }

//    {
//        "index": "music",
//            "type": "song",
//            "suggest": {
//        "text": "张",
//                "title_suggest": {
//            "completion": {
//                "field": "suggest",
//                        "size": 3
//            }
//        }
//    }
//    }
    @ApiOperation(value = "suggest查询", httpMethod = "POST", response = HJResponse.class, notes = "suggest doc")
    @RequestMapping(value = "/suggest", method = RequestMethod.POST)
    public HJResponse suggest(@RequestBody HJSuggestRequest request) {

        return HJResponse.ok(searchService.suggest(request));
    }
}
